package com.qjy.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.example.qjy.korean.R

/**
 * Created by qjy on 2017/12/5.
 */
class KoreanAdapter(val mList: List<String>, val mContext: Context) : RecyclerView.Adapter<KoreanAdapter.KoreanHolder>() {

    var mKoreanLetterOnItemClickListener : KoreanLetterOnItemClickListener ?= null

    override fun onBindViewHolder(holder: KoreanHolder?, position: Int) {
        holder!!.letter.text = mList.get(position);
        holder!!.letter.setOnClickListener {
            mKoreanLetterOnItemClickListener!!.onItemClick(holder!!.letter.text.toString().trim())
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): KoreanHolder {
        var itemView: View = LayoutInflater.from(mContext)
                .inflate(R.layout.item_korean, parent, false)
        var mPlayHistoryHolder = KoreanHolder(itemView)

        return mPlayHistoryHolder
    }

    override fun getItemCount(): Int {
        if (mList.size > 0) {
            return mList.size
        }
        return 0
    }

    class KoreanHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var letter : TextView = itemView.findViewById(R.id.korean_letter)
    }

    fun setKoreanLetterOnItemClickListener(mKoreanLetterOnItemClickListener: KoreanLetterOnItemClickListener){
        this.mKoreanLetterOnItemClickListener = mKoreanLetterOnItemClickListener
    }

    interface KoreanLetterOnItemClickListener{
        fun onItemClick(letter: String)
    }

}